package jdbc.day01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JdbcDemo02 {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement pres=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test","root","123456");
            conn.setAutoCommit(false);
            String st="update student set name='岳宗杰' where id=?";
             pres = conn.prepareStatement(st);
             pres.setInt(1,1);
            int i = pres.executeUpdate();
            System.out.println(i);
            conn.commit();
        } catch (Exception e) {
            try {
                if (conn!=null)
                conn.rollback();
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            if (pres!=null){
                try {
                    pres.close();
                } catch (Exception throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (Exception throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
